/*
 * Copyright 2008 Lukas Vlcek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package org.radio.lastfm.toolbar;

import java.io.IOException;
import org.openide.cookies.InstanceCookie;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
import org.radio.lastfm.control.PlayerButtonControl;
import org.radio.lastfm.control.action.Play;
import org.radio.lastfm.control.action.Tunein;
import org.radio.lastfm.panel.LastFMPanelAction;
import org.radio.lastfm.service.PlayerService;

public class LastFMJPanel extends javax.swing.JPanel {

    private PlayerButtonControl control;
    
    /** Creates new form LastFMJPanel */
    public LastFMJPanel() {
        initComponents();
        
        control = new PlayerButtonControl(this.jbPlay, this.jbSkip, this.jbStop, null);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jbShowPanel = new javax.swing.JButton();
        jbPlay = new javax.swing.JButton();
        jbSkip = new javax.swing.JButton();
        jbStop = new javax.swing.JButton();

        jToolBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar1.setRollover(true);

        jbShowPanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/radio/lastfm/icons/favicon.PNG"))); // NOI18N
        jbShowPanel.setText(org.openide.util.NbBundle.getMessage(LastFMJPanel.class, "LastFMJPanel.jbShowPanel.text")); // NOI18N
        jbShowPanel.setFocusable(false);
        jbShowPanel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbShowPanel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbShowPanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbShowPanelActionPerformed(evt);
            }
        });
        jToolBar1.add(jbShowPanel);

        jbPlay.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/radio/lastfm/icons/control_play.png"))); // NOI18N
        jbPlay.setText(org.openide.util.NbBundle.getMessage(LastFMJPanel.class, "LastFMJPanel.jbPlay.text")); // NOI18N
        jbPlay.setFocusable(false);
        jbPlay.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbPlay.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPlayActionPerformed(evt);
            }
        });
        jToolBar1.add(jbPlay);

        jbSkip.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/radio/lastfm/icons/control_end.png"))); // NOI18N
        jbSkip.setText(org.openide.util.NbBundle.getMessage(LastFMJPanel.class, "LastFMJPanel.jbSkip.text")); // NOI18N
        jbSkip.setFocusable(false);
        jbSkip.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbSkip.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbSkip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSkipActionPerformed(evt);
            }
        });
        jToolBar1.add(jbSkip);

        jbStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/radio/lastfm/icons/control_stop.png"))); // NOI18N
        jbStop.setText(org.openide.util.NbBundle.getMessage(LastFMJPanel.class, "LastFMJPanel.jbStop.text")); // NOI18N
        jbStop.setFocusable(false);
        jbStop.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbStop.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStopActionPerformed(evt);
            }
        });
        jToolBar1.add(jbStop);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void jbPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPlayActionPerformed
    PlayerService.setControl();
    PlayerService.stop(null);
    PlayerService.play(null); 
}//GEN-LAST:event_jbPlayActionPerformed

private void jbSkipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSkipActionPerformed
    PlayerService.setControl();
    PlayerService.stop(null);
    PlayerService.play(null);
}//GEN-LAST:event_jbSkipActionPerformed

private void jbStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbStopActionPerformed
    PlayerService.stop(null);
    PlayerService.setControl(Play.getInstance(), Tunein.getInstance());
}//GEN-LAST:event_jbStopActionPerformed

private void jbShowPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbShowPanelActionPerformed
    LastFMPanelAction action = null;
    try {
        action = (LastFMPanelAction) DataObject.find(
            Repository.getDefault().getDefaultFileSystem().findResource(    
            "Actions/Window/org-radio-lastfm-panel-LastFMPanelAction.instance"))
            .getLookup().lookup(InstanceCookie.class).instanceCreate(); 
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex); 
    } catch (ClassNotFoundException ex) {
        Exceptions.printStackTrace(ex); 
    }
    
    // sanity check...
    if (action == null) {
        action = new LastFMPanelAction();
    }
    
    action.actionPerformed(null); 
}//GEN-LAST:event_jbShowPanelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbPlay;
    private javax.swing.JButton jbShowPanel;
    private javax.swing.JButton jbSkip;
    private javax.swing.JButton jbStop;
    // End of variables declaration//GEN-END:variables

}
